#include "Bookmarks.h"
#include "Log.h"
#include <QtCore/QFile>

QDataStream &operator<<(QDataStream &ds, const Bookmark &b)
{
	ds << b.m_title << b.m_url;
	return ds;
}

QDataStream &operator>>(QDataStream &ds, Bookmark &b)
{
	ds >> b.m_title >> b.m_url;
	return ds;
}

void Bookmarks::remove(const QString &url)
{
  Bookmarks tmp = *this;
  clear();
  for (int i = 0; i < tmp.size(); i++) if (tmp[i].m_url != url)
    *this << tmp[i];
}

void Bookmarks::save(const QString& fName)
{
  QFile file(fName);
  file.open(QIODevice::WriteOnly);
  QDataStream out(&file);   // we will serialize the data into the file
  out << *this; // serialize a vector of bookmarks
  Log::m_log << "save " << this->size() << " bookmarks" << endl;
}

void Bookmarks::load(const QString& fName)
{
  QFile file(fName);
  file.open(QIODevice::ReadOnly);
  if (file.isOpen()) {
    QDataStream in(&file);   // we will serialize the data into the file
    in >> *this; // serialize a vector of bookmarks
  }
  else
    this->clear();
  Log::m_log << "load " << this->size() << " bookmarks" << endl;
}
